//记录玩家的事件

import { ccs } from "../../util/services";
import { game } from "../control/game";

export type recordType = {
    id: string,
    desc: string,
    time: string,
}
export type formatContent = {
    name?: string ,// 谁
    action: string, //干了什么
    location?: string, //地点
    effect?: string, //影响
}
export default class eventMgr {
    
    recordList: recordType[] = [];
    getList() {
        return this.recordList;
    }
    setList(list) {
        this.recordList = list;
    }
    addList(record: formatContent) {
        let content = this.formatRecord(record);
        let idx = this.recordList.findIndex(val => val.id == content.id);
        if(idx >= 0) {
            //添加到
            this.recordList[idx].desc += "\n" +  content.desc;
        } else {
            this.recordList.push(content);
            this.recordList.splice(0, this.recordList.length - 1);
            if(this.recordList.length >= 2) {
            }
        }
        
        ccs.mainEmitter.emit("upRecord");
    }
    formatRecord(desc: formatContent) {
        let str = '';
        if(desc.name) {
            str = desc.name;   
        }
        let descStr = str + (desc.location? "在" + desc.location : "") + desc.action + (desc.effect? "\n" + desc.effect : "");
        let arr = descStr.match(/(\d+)(\.\d+)?/g);
        if(arr) {
            arr.forEach(val => {
                if(val.length >= 5) {
                    descStr = descStr.replace(val, ccs.gf.getOmitNum(val));//("<color=#DE9C1B>" + ccs.gf.getOmitNum(val) + "</color>"));
                }
            })
        }
        return {
            desc: descStr,
            time: game.timerMgr.getTime(),
            id: game.timerMgr.gameTime.year + " " +  game.timerMgr.gameTime.month
        }
    }
}